/**
 * Copyright 2013-2023 Software Radio Systems Limited
 *
 * This file is part of srsRAN.
 *
 * srsRAN is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * srsRAN is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * A copy of the GNU Affero General Public License can be found in
 * the LICENSE file in the top-level directory of this distribution
 * and at http://www.gnu.org/licenses/.
 *
 */

#include "srsran/common/test_common.h"
#include "srsran/rlc/rlc_am_lte.h"
#include <iostream>

// Fixed header only
uint8_t  pdu1[]   = {0x88, 0x06};
uint32_t PDU1_LEN = 2;

// Fixed + 2 LI fields (each 1500)
uint8_t  pdu2[]   = {0x8C, 0x00, 0xDD, 0xC5, 0xDC};
uint32_t PDU2_LEN = 5;

// Fixed + 3 LI fields (each 1500)
uint8_t  pdu3[]   = {0x8C, 0x00, 0xDD, 0xCD, 0xDC, 0x5D, 0xC0};
uint32_t PDU3_LEN = 7;

// D/C = 1 = Data PDU
// RF  = 0 = AMD PDU
// P   = 0 = Status PDU is not requested
// FI  = 11 = First byte of the Data field does not corresponds to the first byte of a RLC SDU,
// Last byte of the Data field does not corresponds to the last byte of a RLC SDU
// E   = 1  = A set of E field and LI field follows from the octet following the fixed part of the header
// SN = 0000000010 -> SN 2
// E   = 1
// LI1 = 1010011 1110 (1342 Dec)
// E  = 0
// LI2 = 10111011100 (1500 Dec)
uint8_t  pdu4[]   = {0x9C, 0x02, 0xD3, 0xE5, 0xDC};
uint32_t PDU4_LEN = 5;

using namespace srsran;

int test1()
{
  srsran::rlc_amd_pdu_header_t h;
  srsran::byte_buffer_t        b1, b2;

  memcpy(b1.msg, &pdu1[0], PDU1_LEN);
  b1.N_bytes = PDU1_LEN;
  rlc_am_read_data_pdu_header(&b1, &h);
  TESTASSERT(RLC_DC_FIELD_DATA_PDU == h.dc);
  TESTASSERT(0x01 == h.fi);
  TESTASSERT(0 == h.N_li);
  TESTASSERT(0 == h.lsf);
  TESTASSERT(0 == h.p);
  TESTASSERT(0 == h.rf);
  TESTASSERT(0 == h.so);
  TESTASSERT(6 == h.sn);
  rlc_am_write_data_pdu_header(&h, &b2);
  TESTASSERT(b2.N_bytes == PDU1_LEN);
  for (uint32_t i = 0; i < b2.N_bytes; i++)
    TESTASSERT(b2.msg[i] == b1.msg[i]);
  return SRSRAN_SUCCESS;
}

int test2()
{
  srsran::rlc_amd_pdu_header_t h;
  srsran::byte_buffer_t        b1, b2;

  memcpy(b1.msg, &pdu2[0], PDU2_LEN);
  b1.N_bytes = PDU2_LEN;
  rlc_am_read_data_pdu_header(&b1, &h);
  TESTASSERT(RLC_DC_FIELD_DATA_PDU == h.dc);
  TESTASSERT(0x01 == h.fi);
  TESTASSERT(2 == h.N_li);
  TESTASSERT(1500 == h.li[0]);
  TESTASSERT(1500 == h.li[1]);
  TESTASSERT(0 == h.lsf);
  TESTASSERT(0 == h.p);
  TESTASSERT(0 == h.rf);
  TESTASSERT(0 == h.so);
  TESTASSERT(0 == h.sn);
  rlc_am_write_data_pdu_header(&h, &b2);
  TESTASSERT(b2.N_bytes == PDU2_LEN);
  for (uint32_t i = 0; i < b2.N_bytes; i++)
    TESTASSERT(b2.msg[i] == b1.msg[i]);
  return SRSRAN_SUCCESS;
}

int test3()
{
  srsran::rlc_amd_pdu_header_t h;
  srsran::byte_buffer_t        b1, b2;

  memcpy(b1.msg, &pdu3[0], PDU3_LEN);
  b1.N_bytes = PDU3_LEN;
  rlc_am_read_data_pdu_header(&b1, &h);
  TESTASSERT(RLC_DC_FIELD_DATA_PDU == h.dc);
  TESTASSERT(0x01 == h.fi);
  TESTASSERT(3 == h.N_li);
  TESTASSERT(1500 == h.li[0]);
  TESTASSERT(1500 == h.li[1]);
  TESTASSERT(1500 == h.li[2]);
  TESTASSERT(0 == h.lsf);
  TESTASSERT(0 == h.p);
  TESTASSERT(0 == h.rf);
  TESTASSERT(0 == h.so);
  TESTASSERT(0 == h.sn);
  rlc_am_write_data_pdu_header(&h, &b2);
  TESTASSERT(b2.N_bytes == PDU3_LEN);
  for (uint32_t i = 0; i < b2.N_bytes; i++)
    TESTASSERT(b2.msg[i] == b1.msg[i]);
  return SRSRAN_SUCCESS;
}

int test4()
{
  srsran::rlc_amd_pdu_header_t h;
  srsran::byte_buffer_t        b1, b2;

  memcpy(b1.msg, &pdu4[0], PDU4_LEN);
  b1.N_bytes = PDU4_LEN;
  rlc_am_read_data_pdu_header(&b1, &h);
  TESTASSERT(RLC_DC_FIELD_DATA_PDU == h.dc);
  TESTASSERT(0x03 == h.fi);
  TESTASSERT(2 == h.N_li);
  TESTASSERT(1342 == h.li[0]);
  TESTASSERT(1500 == h.li[1]);
  TESTASSERT(0 == h.lsf);
  TESTASSERT(0 == h.p);
  TESTASSERT(0 == h.rf);
  TESTASSERT(0 == h.so);
  TESTASSERT(2 == h.sn);
  rlc_am_write_data_pdu_header(&h, &b2);
  TESTASSERT(b2.N_bytes == PDU4_LEN);
  for (uint32_t i = 0; i < b2.N_bytes; i++)
    TESTASSERT(b2.msg[i] == b1.msg[i]);
  return SRSRAN_SUCCESS;
}

int test5()
{
  uint8_t tv[] = {
      0x9a, 0x31, 0xc6, 0xb7, 0x48, 0x61, 0x6e, 0xc3, 0x97, 0x48, 0xa6, 0xe3, 0xac, 0xc0, 0x75, 0x9f, 0xb2, 0xc8, 0xed,
      0xb0, 0xad, 0xcc, 0xc2, 0x0d, 0x28, 0xdd, 0xbb, 0x4b, 0x4c, 0xc5, 0xfc, 0x52, 0x40, 0xc2, 0x09, 0x89, 0x23, 0x77,
      0x4c, 0xba, 0xbf, 0xf2, 0x9d, 0x6b, 0xb2, 0x12, 0x0b, 0x64, 0xda, 0xf8, 0x14, 0x88, 0xc4, 0xd7, 0x95, 0xec, 0xb4,
      0x50, 0x37, 0x00, 0x15, 0x33, 0x52, 0x56, 0xf5, 0x5b, 0xdf, 0x18, 0xd2, 0x2b, 0xd2, 0x92, 0x1d, 0x6f, 0xfd, 0xcf,
      0x82, 0x08, 0x33, 0x5c, 0x00, 0x48, 0xe4, 0xc4, 0x1f, 0x79, 0xb0, 0xd3, 0xca, 0xe8, 0xd3, 0xdf, 0x1b, 0x25, 0x35,
      0x11, 0x80, 0x14, 0x29, 0x52, 0x3f, 0xfc, 0xe4, 0x5c, 0x6b, 0xe2, 0x2b, 0xed, 0xea, 0x5f, 0x4a, 0xeb, 0xa7, 0x2e,
      0xaf, 0xc6, 0xa8, 0x60, 0x99, 0x72, 0x48, 0x6c, 0x51, 0x63, 0x91, 0x87, 0x74, 0x11, 0x9b, 0x9e, 0x63, 0xdb, 0x9a,
      0x48, 0x37, 0x05, 0x2a, 0x63, 0xf3, 0x14, 0xc2, 0x3d, 0xff, 0x69, 0x6b, 0xaf, 0x2f, 0x13, 0x0f, 0xc8, 0x85, 0x57,
      0x34, 0xd7, 0xba, 0xc5, 0x5e, 0x2f, 0xd6, 0xf9, 0xcd, 0x39, 0xd4, 0x67, 0x81, 0x0c, 0x6c, 0xab, 0x6f, 0x5f, 0xc0,
      0x31, 0x9c, 0xbf, 0x9a, 0x08, 0x6e, 0xc9, 0x1b, 0x7d, 0x91, 0xa1, 0xd4, 0xc5, 0x78, 0x8f, 0x8a, 0xd6, 0xbe, 0x60,
      0xcf, 0x8b, 0x99, 0xa4, 0xf2, 0x1b, 0xb0, 0x5e, 0xc6, 0x1f, 0xbe, 0x86, 0x50, 0x5a, 0x46, 0xea, 0x62, 0xb4, 0xb3,
      0x7e, 0x32, 0x44, 0x1f, 0x06, 0x09, 0x97, 0x95, 0x93, 0x6d, 0x53, 0xf3, 0x3c, 0xde, 0x8c, 0xe0, 0xd0, 0xa7, 0x90,
      0x2f, 0x6e, 0xaf, 0xed, 0xf4, 0xff, 0x47, 0x3a, 0xe9, 0xaa, 0xef, 0x9c, 0x28, 0x21, 0xe0, 0x47, 0x27, 0xe9, 0xde,
      0xbd, 0x7c, 0x4b, 0x10, 0x6f, 0x87, 0xef, 0xfc, 0x68, 0xbf, 0xa3, 0xf8, 0xee, 0x11, 0xa8, 0xdb, 0x06, 0xa7, 0x23,
      0x40, 0x91, 0xcd, 0x2f, 0x2d, 0xf5, 0x50, 0x0e, 0x3c, 0x78, 0xf7, 0x1a, 0x35, 0x74, 0x65, 0x45, 0xe3, 0xec, 0x34,
      0xdf, 0x54, 0xf4, 0x83, 0x4d, 0xe2, 0x94, 0xf5, 0xbe, 0x9a, 0x9c, 0xe1, 0xdb, 0x2d, 0xae, 0x0a, 0x5b, 0xa3, 0x5b,
      0x69, 0xdf, 0xd3, 0x60, 0xf9, 0x08, 0xd4, 0x5e, 0x4d, 0xb8, 0x4a, 0x82, 0x97, 0x9f, 0x76, 0x1a, 0xec, 0x58, 0xaf,
      0xe1, 0x16, 0x49, 0x7d, 0xf7, 0x24, 0xab, 0xa5, 0x2f, 0x06, 0x48, 0x8a, 0x6f, 0x27, 0x5d, 0xcf, 0x20, 0x65, 0xa4,
      0x7e, 0xb2, 0x5c, 0xc9, 0x34, 0xf3, 0x68, 0xaa, 0x0e, 0x54, 0x03, 0xbd, 0x35, 0x19, 0x06, 0xb2, 0x11, 0x2b, 0x5d,
      0xb6, 0x5a, 0x63, 0xff, 0xe4, 0xd2, 0x26, 0x41, 0xa2, 0x47, 0xa6, 0x46, 0xc5, 0x58, 0xa2, 0x8e, 0x8d, 0x95, 0xf6,
      0x37, 0xa3, 0x4a, 0x3a, 0x60, 0x7f, 0x54, 0x67, 0x32, 0x65, 0x92, 0x8f, 0x1b, 0xec, 0xf3, 0x1a, 0xd0, 0xc5, 0x41,
      0x11, 0x67, 0x88, 0xb7, 0xad, 0x4d, 0x0f, 0x4f, 0xdc, 0x9c, 0xe5, 0xd2, 0xd4, 0x88, 0x1d, 0x0e, 0xe9, 0x9c, 0x62,
      0x50, 0xce, 0xc7, 0xe2, 0x5e, 0xe3, 0xce, 0x51, 0xfd, 0x9e, 0x16, 0x3e, 0xaf, 0x7e, 0xc6, 0x66, 0x2b, 0x14, 0x75,
      0x7b, 0xf0, 0x12, 0x60, 0xc2, 0xe6, 0xe8, 0xdf, 0xf4, 0xd1, 0x7c, 0x57, 0x21, 0x4a, 0x1e, 0x03, 0xa8, 0x01, 0xd1,
      0xf9, 0xff, 0x6f, 0x10, 0x3d, 0x1e, 0x8e, 0x04, 0x84, 0xb9, 0x18, 0xfa, 0x34, 0x08, 0x0c, 0x94, 0xca, 0xf2, 0x7d,
      0xaa, 0xe6, 0x4e, 0x26, 0x3d, 0x70, 0x70, 0x5c, 0x73, 0x19, 0x5d, 0x45, 0x12, 0x5c, 0xb4, 0x22, 0x9a, 0xd3, 0xb0,
      0x9e, 0x57, 0x6a, 0xb6, 0x51, 0x9e, 0xbe, 0x5d, 0x33, 0x88, 0x4f, 0xb0, 0x32, 0x36, 0xfe, 0x58, 0x73, 0x6e, 0xc9,
      0xcf, 0xe2, 0xe2, 0x2d, 0x27, 0xf4, 0x89, 0xdb, 0x17, 0x23, 0xae, 0xc7, 0xc1, 0x06, 0x31, 0x77, 0x57, 0xd0, 0x35,
      0xb5, 0x03, 0xbe, 0x04, 0xb3, 0xf0, 0x3a, 0xb1, 0x49, 0xae, 0x20, 0x12, 0x7d, 0x02, 0xf4, 0xaa, 0x29, 0xe8, 0x34,
      0x04, 0xff, 0x57, 0xb3, 0xc7, 0x19, 0xb9, 0xf8, 0x90, 0x10, 0xc8, 0xc6, 0xc5, 0xcb, 0x84, 0xca, 0x7e, 0x74, 0x04,
      0x30, 0xbd, 0xb2, 0x50, 0xcf, 0x30, 0x52, 0xc3, 0xda, 0x7b, 0xac, 0x0e, 0x7f, 0xab, 0x66, 0x32, 0x72, 0x7f, 0xeb,
      0x6b, 0x0f, 0xfc, 0x33, 0xd5, 0xc1, 0xff, 0x59, 0x8b, 0x7d, 0xce, 0x90, 0xad, 0x8b, 0x42, 0xfd, 0x5b, 0x72, 0x4f,
      0x1e, 0x4d, 0xca, 0xca, 0x5b, 0x4a, 0x76, 0xc1, 0x7c, 0xe8, 0x40, 0x68, 0x53, 0x50, 0x64, 0x87, 0x25, 0x25, 0x86,
      0x7f, 0xb1, 0x03, 0x4d, 0x41, 0xb1, 0xd8, 0x83, 0xae, 0x33, 0xf6, 0xfe, 0x52, 0x43, 0xc8, 0x1c, 0x9e, 0x12, 0x92,
      0x60, 0x8f, 0x7b, 0xa0, 0xf7, 0xce, 0xf0, 0x5b, 0x55, 0x16, 0x80, 0xdb, 0x95, 0x31, 0xdf, 0xe2, 0x72, 0x90, 0xba,
      0xf6, 0x3e, 0xee, 0xec, 0x3c, 0x40, 0x2f, 0x05, 0x5c, 0xcd, 0x17, 0xef, 0x2d, 0xa6, 0x6a, 0xce, 0x9d, 0x38, 0xbe,
      0xf8, 0x8e, 0xd4, 0x79, 0x69, 0x69, 0xaa, 0x48, 0x4b, 0x1d, 0xd8, 0x06, 0x13, 0x17, 0xf4, 0xff, 0x53, 0x34, 0x2e,
      0x58, 0x90, 0xfb, 0x70, 0x7f, 0x29, 0x16, 0xe9, 0xf7, 0xb4, 0x22, 0xb5, 0xac, 0xb0, 0x8a, 0x25, 0x19, 0xf3, 0xd0,
      0x62, 0x3f, 0xed, 0x3a, 0x45, 0x00, 0x51, 0x39, 0xff, 0xa5, 0x6d, 0x2d, 0xfd, 0xfd, 0x28, 0x6d, 0x7d, 0x51, 0x84,
      0x66, 0x48, 0x38, 0x88, 0xfe, 0xe4, 0x38, 0x88, 0x0a, 0x52, 0x7b, 0xda, 0xb4, 0xba, 0xc7, 0xee, 0xff, 0xc7, 0x40,
      0x38, 0xc6, 0xe5, 0xa5, 0xf3, 0xe2, 0xa3, 0x1b, 0x50, 0x20, 0x6d, 0xd4, 0x86, 0xb5, 0x0c, 0x0f, 0xb3, 0xf0, 0x47,
      0x3b, 0xfa, 0x99, 0xb7, 0xd4, 0x4d, 0x71, 0x9b, 0x3c, 0x71, 0x62, 0x7c, 0xa9, 0x28, 0x61, 0x4f, 0x1b, 0x43, 0xf2,
      0x37, 0x93, 0x12, 0xa4, 0x67, 0x98, 0x59, 0x73, 0xa7, 0x0d, 0x64, 0xef, 0x48, 0x5e, 0x88, 0xff, 0x33, 0xd6, 0x71,
      0xce, 0x12, 0xe2, 0x31, 0x8e, 0x8b, 0x59, 0xef, 0xda, 0x75, 0x32, 0xcc, 0xac, 0xc6, 0xde, 0x50, 0x2d, 0x77, 0xa9,
      0xa1, 0x1e, 0xb6, 0x05, 0x0d, 0xff, 0x63, 0x96, 0xfe, 0x96, 0x6c, 0x6f, 0x65, 0x7e, 0x51, 0x96, 0x0c, 0xdd, 0xef,
      0xfb, 0xb7, 0x64, 0x2d, 0x84, 0x10, 0xf3, 0x62, 0x60, 0x21, 0xd9, 0x0a, 0xc2, 0xf8, 0xc0, 0xc7, 0x05, 0xbf, 0x2a,
      0x9b, 0xbe, 0xc1, 0x07, 0x2d, 0x26, 0x85, 0x7f, 0xbc, 0x91, 0x5c, 0xab, 0x8c, 0x13, 0x10, 0xba, 0x97, 0x20, 0xad,
      0xfa, 0x81, 0xce, 0xd3, 0x8b, 0x90, 0xcb, 0x4b, 0x57, 0xd1, 0x0b, 0x82, 0x6c, 0xc9, 0x43, 0x74, 0xf6, 0x69, 0xf9,
      0x75, 0x25, 0x8b, 0xd1, 0xd0, 0x17, 0xe5, 0xe0, 0xd1, 0x7c, 0x01, 0x7f, 0x76, 0x82, 0x4d, 0x4a, 0x0d, 0xde, 0x15,
      0x58, 0x35, 0xe6, 0x63, 0xb7, 0x53, 0x2c, 0xfa, 0xc7, 0x23, 0x63, 0xc0, 0x98, 0x88, 0x4b, 0x6a, 0x59, 0x63, 0x4f,
      0x39, 0x34, 0xcb, 0x3a, 0xb3, 0x42, 0xbc, 0x01, 0x8c, 0xc9, 0xdf, 0xa1, 0x22, 0x14, 0x88, 0x85, 0xcc, 0xdb, 0xb2,
      0xc6, 0xa2, 0xd5, 0x2a, 0x62, 0x6d, 0xb2, 0xae, 0xd7, 0x0b, 0x11, 0x26, 0x45, 0x45, 0xf2, 0x7f, 0xf9, 0x34, 0x3c,
      0xfa, 0xc0, 0x05, 0xd9, 0x61, 0x27, 0xed, 0xe9, 0xad, 0xb9, 0xc4, 0x5f, 0x80, 0x66, 0x34, 0xaa, 0xc9, 0xa1, 0x5c,
      0x77, 0x79, 0x68, 0x88, 0x9f, 0xad, 0xcd, 0x91, 0x2c, 0xc6, 0xc5, 0x68, 0xc0, 0x85, 0x6e, 0x99, 0xe7, 0x95, 0x87,
      0xd0, 0x42, 0x40, 0x95, 0xa1, 0xc0, 0xfb, 0xd5, 0x6a, 0xc4, 0x77, 0xc7, 0x3b, 0xf0, 0x2f, 0xc3, 0x8f, 0xdc, 0x91,
      0x21, 0x08, 0x57};

  srsran::rlc_amd_pdu_header_t h;
  srsran::byte_buffer_t        b1;

  memcpy(b1.msg, tv, sizeof(tv));
  b1.N_bytes                 = sizeof(tv);
  uint32_t nof_payload_bytes = sizeof(tv);
  rlc_am_read_data_pdu_header(&b1.msg, &nof_payload_bytes, &h);

  TESTASSERT(nof_payload_bytes == 1046);
  TESTASSERT(RLC_DC_FIELD_DATA_PDU == h.dc);
  TESTASSERT(RLC_FI_FIELD_NOT_START_OR_END_ALIGNED == h.fi);
  TESTASSERT(0 == h.N_li);
  TESTASSERT(0 == h.lsf);
  TESTASSERT(0 == h.p);
  TESTASSERT(0 == h.rf);
  TESTASSERT(0 == h.so);
  TESTASSERT(561 == h.sn);

  return SRSRAN_SUCCESS;
}

int main(int argc, char** argv)
{
  srslog::init();

  TESTASSERT(test1() == SRSRAN_SUCCESS);
  TESTASSERT(test2() == SRSRAN_SUCCESS);
  TESTASSERT(test3() == SRSRAN_SUCCESS);
  TESTASSERT(test4() == SRSRAN_SUCCESS);
  TESTASSERT(test5() == SRSRAN_SUCCESS);

  return SRSRAN_SUCCESS;
}
